/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AggregatedSubscriptionTest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form, GrooveWebServicesV12Helpers.Shared.IGrooveWebServicesAdvancedEventCallback
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private GrooveWebServicesV12Helpers.Account m_Account;
		private GrooveWebServicesV12Helpers.Identity m_Identity;
		private GrooveWebServicesV12Helpers.Context m_Context;

		private string m_AggregatedFilesSubscriptionID;
		private string m_AggregatedFormsSubscriptionID;

		private const string EVENT_CALLBACK_URI = @"dpp://localhost/GrooveWebServicesHelpers/Event_SomeCallbackURI";
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox FilesSubscriptionIDTextBox;
		private System.Windows.Forms.TextBox FormsSubscriptionIDTextBox;
		private System.Windows.Forms.Label label3;
		private const string CUSTOMIZABLE_DISCUSSION_QUERY = "DesignName=urn:groove.net.platform.tools.forms.CustomizableDiscussion";

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}

				if (!System.String.IsNullOrEmpty(m_AggregatedFilesSubscriptionID))
				{
					m_Identity.RemoveAggregatedToolEventListener
						(
						m_AggregatedFilesSubscriptionID,
						this
						);

					m_AggregatedFilesSubscriptionID = null;
				}

				if (!System.String.IsNullOrEmpty(m_AggregatedFormsSubscriptionID))
				{
					m_Identity.RemoveAggregatedToolEventListener
						(
						m_AggregatedFormsSubscriptionID,
						this
						);

					m_AggregatedFormsSubscriptionID = null;
				}

			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.FilesSubscriptionIDTextBox = new System.Windows.Forms.TextBox();
			this.FormsSubscriptionIDTextBox = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(136, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Files Subscription ID:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(136, 24);
			this.label2.TabIndex = 1;
			this.label2.Text = "Forms Subscription ID:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// FilesSubscriptionIDTextBox
			// 
			this.FilesSubscriptionIDTextBox.Location = new System.Drawing.Point(144, 8);
			this.FilesSubscriptionIDTextBox.Name = "FilesSubscriptionIDTextBox";
			this.FilesSubscriptionIDTextBox.ReadOnly = true;
			this.FilesSubscriptionIDTextBox.Size = new System.Drawing.Size(328, 20);
			this.FilesSubscriptionIDTextBox.TabIndex = 2;
			this.FilesSubscriptionIDTextBox.Text = "";
			// 
			// FormsSubscriptionIDTextBox
			// 
			this.FormsSubscriptionIDTextBox.Location = new System.Drawing.Point(144, 32);
			this.FormsSubscriptionIDTextBox.Name = "FormsSubscriptionIDTextBox";
			this.FormsSubscriptionIDTextBox.ReadOnly = true;
			this.FormsSubscriptionIDTextBox.Size = new System.Drawing.Size(328, 20);
			this.FormsSubscriptionIDTextBox.TabIndex = 3;
			this.FormsSubscriptionIDTextBox.Text = "";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(8, 80);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(464, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "See debug output window for information when events are detected";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(480, 102);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.FormsSubscriptionIDTextBox);
			this.Controls.Add(this.FilesSubscriptionIDTextBox);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form1";
			this.Text = "Aggregated Subscription Test";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			m_Context = new GrooveWebServicesV12Helpers.Context();
			m_Account = GrooveWebServicesV12Helpers.Account.GetAccounts(m_Context)[0];
			m_Identity = m_Account.Identities[0];
		
			m_AggregatedFilesSubscriptionID = m_Identity.AddAggregatedToolEventListener
				(
				GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_FILES_EVENT_CLASS,
				EVENT_CALLBACK_URI,
				"",
				1,
				this
				);

			FilesSubscriptionIDTextBox.Text = m_AggregatedFilesSubscriptionID.ToString();

			m_AggregatedFormsSubscriptionID = m_Identity.AddAggregatedToolEventListener
				(
				GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_FORMS2_EVENT_CLASS,
				EVENT_CALLBACK_URI, 
				CUSTOMIZABLE_DISCUSSION_QUERY,
				1,
				this
				);

			FormsSubscriptionIDTextBox.Text = m_AggregatedFormsSubscriptionID.ToString();
		}
	
	
		#region IGrooveWebServicesAdvancedEventCallback Members

		public void OnSubscriptionUpdated(GrooveWebServicesV12Helpers.Subscription i_Subscription)
		{
			System.Diagnostics.Debug.WriteLine("Subscription updated: ID='" + i_Subscription.SubscriptionID + "', EventClass: " + i_Subscription.EventClass);
		}

		public void OnEventProcessingCompleted(GrooveWebServicesV12Helpers.Subscription i_Subscription, long i_SequenceNumber)
		{
			System.Diagnostics.Debug.WriteLine("Event processing completed: ID='" + i_Subscription.SubscriptionID + "', SequenceNumber: " + i_SequenceNumber);
		}

		public void OnEventsDeleted(GrooveWebServicesV12Helpers.Subscription i_Subscription, long i_SequenceNumber)
		{
			System.Diagnostics.Debug.WriteLine("Events deleted: ID='" + i_Subscription.SubscriptionID + "', SequenceNumber: " + i_SequenceNumber);
		}

		#endregion

		#region IGrooveWebServicesEventCallback Members

		public void ProcessEvent(GrooveWebServicesV12Helpers.GrooveEventsWebService.Event i_Event)
		{
			System.Diagnostics.Debug.WriteLine("Event - Subscription: " + i_Event.SubscriptionID + ", EventClass: " + i_Event.EventClass + ", EventType:" + i_Event.EventType);
		}

		#endregion
	}
}
